package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.UriUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.midi.lib.mid.MidiFile;
import com.stark.midi.lib.model.MidFileBean;
import com.stark.midi.lib.model.MidFileProvider;
import com.stark.midi.lib.model.MidImportFileManager;
import com.stark.piano.lib.widget.PianoSongPlayer;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.MusicListAdapter;
import flc.ast.adapter.MusicTitleAdapter;
import flc.ast.adapter.SelAudioAdapter;
import flc.ast.databinding.ActivityMusicListBinding;
import flc.ast.utils.Content;
import stark.common.basic.utils.IOUtil;
import stark.common.basic.utils.UriUtil;

public class MusicListActivity extends BaseAc<ActivityMusicListBinding> {

    private MusicTitleAdapter musicTitleAdapter;
    private MusicListAdapter musicListAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_music_list;
    }

    @Override
    protected void initView() {
        mDataBinding.ivMusicListBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivMusicImport.setOnClickListener(this);
        mDataBinding.rvMusicListTitle.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        musicTitleAdapter = new MusicTitleAdapter();
        mDataBinding.rvMusicListTitle.setAdapter(musicTitleAdapter);
        musicTitleAdapter.setOnItemClickListener(this);

        mDataBinding.rvMusicList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        musicListAdapter = new MusicListAdapter();
        mDataBinding.rvMusicList.setAdapter(musicListAdapter);
        musicListAdapter.addChildClickViewIds(R.id.ivMusicListItemType1);
        musicListAdapter.addChildClickViewIds(R.id.ivMusicListItemType2);
        musicListAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivMusicImport:
                Intent intent = new Intent(this, SelAudioActivity.class);
                startActivityForResult(intent, 110);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        musicTitleAdapter.setSelPos(position);
        musicTitleAdapter.notifyDataSetChanged();
        mDataBinding.rvMusicList.setVisibility(View.VISIBLE);
        mDataBinding.ivNoData.setVisibility(View.GONE);
        switch (position) {
            case 0:
                musicListAdapter.setList(MidFileProvider.getHot());
                break;
            case 1:
                musicListAdapter.setList(MidFileProvider.getChildrenSong());
                break;
            case 2:
                musicListAdapter.setList(MidFileProvider.getClassic());
                break;
            case 3:
                musicListAdapter.setList(MidFileProvider.getDance());
                break;
            case 4:
                musicListAdapter.setList(MidFileProvider.getMore());
                break;
            case 5:
                List<MidFileBean> fileBeanList = MidImportFileManager.getInstance().getFileBeanList();
                if (fileBeanList.size() == 0) {
                    mDataBinding.ivNoData.setVisibility(View.VISIBLE);
                    mDataBinding.rvMusicList.setVisibility(View.GONE);
                }
                musicListAdapter.setList(fileBeanList);
                break;
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()){
            case R.id.ivMusicListItemType1:
                PianoTrainActivity.pianoTrainMode= PianoSongPlayer.Mode.APPRECIATE;
                PianoTrainActivity.playFileBean=musicListAdapter.getItem(position);
                startActivity(PianoTrainActivity.class);
                break;
            case R.id.ivMusicListItemType2:
                PianoTrainActivity.pianoTrainMode= PianoSongPlayer.Mode.PRACTICE;
                PianoTrainActivity.playFileBean=musicListAdapter.getItem(position);
                startActivity(PianoTrainActivity.class);
                break;
        }
    }

    @Override
    protected void initData() {
        List<String> titleList = new ArrayList<>();
        titleList.add(getString(R.string.music_type1));
        titleList.add(getString(R.string.music_type2));
        titleList.add(getString(R.string.music_type3));
        titleList.add(getString(R.string.music_type4));
        titleList.add(getString(R.string.music_type5));
        titleList.add(getString(R.string.music_type6));
        musicTitleAdapter.setSelPos(0);
        musicTitleAdapter.setList(titleList);
        musicListAdapter.setList(MidFileProvider.getHot());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == 110) {
            String uri = data.getStringExtra(Content.videoUri);
            List<MidFileBean> fileBeanList = new ArrayList<>();
            fileBeanList.add(createFromUri(Uri.parse(uri)));
            MidImportFileManager.getInstance().addMidFileBeans(fileBeanList);
            musicTitleAdapter.setSelPos(5);
            musicTitleAdapter.notifyDataSetChanged();
            List<MidFileBean> fileBeanList1 = MidImportFileManager.getInstance().getFileBeanList();
            if (fileBeanList.size() == 0) {
                mDataBinding.ivNoData.setVisibility(View.VISIBLE);
                mDataBinding.rvMusicList.setVisibility(View.GONE);
            }
            musicListAdapter.setList(fileBeanList1);
        }
    }

    private MidFileBean createFromUri(Uri uri) {
        try {
            InputStream inputStream = getContentResolver().openInputStream(uri);
            MidiFile midiFile = new MidiFile(inputStream);
            IOUtil.close(inputStream);
            if (midiFile.getTrackCount() <= 0) {
                //非正常的mid文件
                return null;
            }

            MidFileBean fileBean = new MidFileBean();
            fileBean.setFileUriStr(uri.toString());
            File file = UriUtils.uri2File(uri);
            if (file != null) {
                fileBean.setFileName(FileUtils.getFileNameNoExtension(file));
            } else {
                fileBean.setFileName(UriUtil.getFileNameByUri(uri));
            }
            return fileBean;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}